package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.mappers.UserMapper;
import com.tanhua.model.db.User;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class UserApiImpl implements UserApi {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByMobile(String mobile) {
        //使用mybatis-plus查询
        QueryWrapper<User> qw = new QueryWrapper<>();
        //select * from tb_user where mobile=?
        qw.eq("mobile",mobile);//条件字段，比较的数据
        return userMapper.selectOne(qw);
    }

    //保存用户
    public Long save(User user) {
        userMapper.insert(user);
        System.out.println("调用提供者保存用户id："+user.getId());
        return user.getId();
    }

    //更新
    public void update(User user) {
        userMapper.updateById(user);
    }

    //查询全部
    public List<User> findAll() {
        return userMapper.selectList(null);
    }

    //根据userId查询User
    public User findById(Long userId) {
        return userMapper.selectById(userId);
    }
}
